package project.ccso.top.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.ReturnResult;
import project.ccso.top.entity.OneCategory;
import project.ccso.top.entity.TwoCategory;
import project.ccso.top.enumerate.Result;
import project.ccso.top.service.TwoCategoryService;
import project.ccso.top.service.impl.TwoCategoryServiceImpl;

/**
 * TwoCategoryServlet
 * 处理与TwoCategory相关的请求
 * @author aibinxiao
 * @date 2018年8月23日 下午3:17:20
 */
@WebServlet("/TwoCategoryServlet")
public class TwoCategoryServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static ObjectMapper mapper = new ObjectMapper();
	private static Logger logger = LoggerFactory.getLogger(TwoCategoryServlet.class);
	private TwoCategoryService twoCategoryService = new TwoCategoryServiceImpl();
	private TwoCategory twoCategory = null;
	private List<TwoCategory>list = null;
	
	//显示二级分类信息，支持搜索
	public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String pageNo = request.getParameter("page"); // pageNum 页码
		String pageSize = request.getParameter("rows"); // pageSize 每页条数
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String oneCategoryId = request.getParameter("oneCategoryId");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		GridPageBean<TwoCategory>pageBean = null;
		try {
			pageBean = twoCategoryService.findByPage(pageNo, pageSize, id, name, code,oneCategoryId,startDate, endDate);
			result.setResult(Result.SUCCESS.name());
			result.setMessage("查询第" + pageNo + "页的二级分类信息成功！");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(pageBean);
		writer.print(mapper.writeValueAsString(result));	
	}
	
	//编辑信息回显
		public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			PrintWriter writer = response.getWriter();
			ReturnResult result = new ReturnResult();
			int id = Integer.parseInt(request.getParameter("id"));
			try {
				twoCategory = twoCategoryService.findCate(id);
				result.setResult(Result.SUCCESS.name());
				result.setMessage("进入编辑编号为" + id + "的二级分类信息成功！");
			} catch (Exception e) {
				result.setResult(Result.FAILED.name());
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
			logger.info("twoCategory");
			result.setData(twoCategory);
			writer.print(mapper.writeValueAsString(result));
		}
		
		//修改和增加二级分类信息
		public void twoCateUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			PrintWriter writer = response.getWriter();
			ReturnResult result = new ReturnResult();
			String id = request.getParameter("id");
			String code = request.getParameter("code");
			String name = request.getParameter("name");
			String desc = request.getParameter("desc");
			String oneCategoryId = request.getParameter("oneCategoryId");
			boolean boolResult = false;
			try {	
				 boolResult = twoCategoryService.UpdateCate(id,code,name,desc,oneCategoryId);
				 if (boolResult) {
					result.setResult(Result.SUCCESS.name());
					if(StringUtils.isEmpty(id)) {
						result.setMessage("添加名称为" + name + "的二级分类信息成功！");
					}else {
						result.setMessage("修改Id为" + id + "的二级分类信息成功！");
					}
					logger.info(result.getMessage());
				} 
			} catch (Exception e) {
				result.setResult(Result.FAILED.name());
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
			result.setData(boolResult);
			writer.print(mapper.writeValueAsString(result));
		}
	
}
